﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SecurityEvaluate;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Web.UI.DataVisualization.Charting;
using System.Data;

public partial class Police_SecurityEvaluate_SquadronAnlysediary : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDateList();
            if (Request["Departmentid"] != null)
            {

                DptMonth.SelectedValue = Request["m"];
                DptYear.SelectedValue = Request["y"];
            }
            BindDate();
        }
    }
    DiaryDataContext diarydb = new DiaryDataContext();
    DutyLogDataContext dutydb = new DutyLogDataContext();
    ParkLogDataContext parkdb = new ParkLogDataContext();

    private void BindDate()
    {

        string department = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Request["Departmentid"] != null)
        {
            department = Request["Departmentid"];

        }
        Caption.Text = "（" + Yipusoft.Account.BLL.Department.GetDepartment(department).ShortName + "）要事日记统计分析";
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("department"));
        dt.Columns.Add(new DataColumn("departmentid"));
        dt.Columns.Add(new DataColumn("diary", typeof(decimal)));


        int year = Convert.ToInt32(DptYear.SelectedValue);
        int month = Convert.ToInt32(DptMonth.SelectedValue);
        int monthDayCount = Account.GetMonthDayCount(year, month);
        //总数
        int diaryTotal = monthDayCount;

        List<DepartmentInfo> departments = Department.GetList("departmentid like '" + department.Substring(0, 4) + "3%' and state=0 ");
        foreach (DepartmentInfo info in departments)
        {

            DataRow dr = dt.NewRow();
            dr["department"] = info.ShortName;
            dr["departmentid"] = info.DepartmentID;
            int departmentCount = 1;
            //要是日记
            //int diarysum = diarydb.DiarySquadron.Where(o => ).Count();
            int diarysum = (from o in diarydb.DiarySquadron where o.DepartmentId == info.DepartmentID && o.DiaryDate.Value.Year == year && o.DiaryDate.Value.Month == month group o by new { o.DepartmentId, o.DiaryDate } into k select k.Key).Count();

            if (departmentCount * diaryTotal == 0)
            {
                dr["diary"] = DBNull.Value;
                // UserChart.Series["要是日记"].Points.AddXY(info.AliasName, DBNull.Value);
            }
            else
            {
                decimal agv = (decimal)(departmentCount * diaryTotal - diarysum) / (departmentCount * diaryTotal);
                // UserChart.Series["要是日记"].Points.AddXY(info.AliasName, agv / 3);
                dr["diary"] = agv;
            }



            dt.Rows.Add(dr);
        }

        DataView dv = dt.DefaultView;
        dv.Sort = " diary desc";


        UserChart.Series["要事日记"].Points.DataBindXY(dv, "department", dt.DefaultView, "diary");
        UserChart.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = false;
        UserChart.Series["要事日记"].LabelFormat = "0.0" + "%";
        for (int i = 0; i < dv.Count; i++)
        {
            UserChart.Series["要事日记"].Points[i].Url = "List.aspx?Departmentid=" + Convert.ToString(dv[i][1]) + "&y=" + DptYear.SelectedValue + "&m=" + DptMonth.SelectedValue;
        }
    }


    private void BindDateList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            DptYear.Items.Add(li);
        }
        for (int i = 1; i <= 12; i++)
        {
            this.DptMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }
        if (Request.QueryString["year"] == null)
        {
            DptMonth.SelectedValue = DateTime.Now.Month.ToString();
            DptYear.SelectedValue = DateTime.Now.Year.ToString();
        }
        else
        {
            DptMonth.SelectedValue = Request["month"];
            DptYear.SelectedValue = Request["year"];
        }
    }

    protected void DptYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDate();
    }
    protected void DptMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDate();
    }
    protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDate();
    }
}
